package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.PurchaseInfoBean;
import com.project.bean.PurchaseOrderBean;
import com.project.mapper.IPurchaseOrderMapper;
import com.project.service.IPurchaseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

/**
 * 采购订单业务方法
 */
@Service
@Transactional
public class PurchaseOrderServiceImpl implements IPurchaseOrderService {
    @Autowired
    private IPurchaseOrderMapper purchaseOrderMapper;
    //动态分页查询采购订单
    @Override
    public IPage<PurchaseOrderBean> findByItem(Integer pageNO, String supplierName,
                                               LocalDate startDate, LocalDate endDate) {
        QueryWrapper<PurchaseOrderBean>qw=new QueryWrapper<>();
        if (supplierName!=null&&supplierName.length()!=0){
            qw.like("po_supplierName",supplierName);
        }
        if (startDate!=null){
            qw.ge("po_inDate",startDate);
        }
        if (endDate!=null){
            qw.le("po_inDate",endDate);
        }
        return purchaseOrderMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
    public PurchaseOrderBean findById(Integer id) {
        //通过订单id查询订单信息，同时查询该订单的明细信息。在采购明细信息中，需要查询商品名称和品牌。
        PurchaseOrderBean purchaseOrder= purchaseOrderMapper.selectById(id);//查询订单信息
        List<PurchaseInfoBean>list =purchaseOrderMapper.findById(id);//查询该订单的明细信息。在采购明细信息中，需要查询商品名称和品牌。
        purchaseOrder.setPurchaseInfoList(list);
        return purchaseOrder;
    }
}
